{extend name="base"/}
{block name="title"}
<title>{$config.seo_title}</title>
<meta name="keywords" content="{$config.seo_keywords}" />
<meta name="description" content="{$config.seo_description}" />
{/block}
{block name="style"}{/block}
{block name="name"}注册{/block}

{block name="content"}
<div class="top_img">
    <img src="__HTML__/images/bkc.png" alt="">
</div>
<div class="input">
    <!--<form action="/register.html" method="post">-->
    <div class="under">
        <input type="text" class="admin" placeholder="请输入手机号" name="username">
        <div class="speil">
            <input type="text" class="pwd" placeholder="验证码" name="code">
            <input id="btnSendCode" type="button" value="获取验证码" onClick="sendMessage()" />
            <!--<button onclick="sendCode()">发送验证码</button>-->
        </div>
        <input type="text" class="pwd" placeholder="请填写密码" name="password">
    </div>
    <input type="button" class="submit" value="注册" onclick="formSubmit()">
    <!--</form>-->
    <p>
        已注册？<a href="/login.html">请登录</a>
    </p>
</div>
{/block}
{block name="footer"}{/block}
{block name="js"}
<script type="text/javascript" src="__ORG__/layer/layer.js"></script>
<script>
    var InterValObj; //timer变量，控制时间
    var count = 120; //间隔函数，1秒执行
    var curCount;//当前剩余秒数
    var code = ""; //验证码
    var codeLength = 6;//验证码长度
    function sendMessage() {
        curCount = count;
        var dealType; //验证方式
        var username=$("input[name='username']").val();
        if (username == '' || username == undefined || !(/^1[34578]\d{9}$/.test(username))) {
            layer.msg('手机号码输入有误',{time:3000});
            return false;
        }
        if ($("#phone").attr("checked") == true) {
            dealType = "phone";
        }
        else {
            dealType = "email";
        }
        //产生验证码
        for (var i = 0; i < codeLength; i++) {
            code += parseInt(Math.random() * 9).toString();
        }
        //设置button效果，开始计时
        /*$("#btnSendCode").attr("disabled", "true");
        $("#btnSendCode").val( + curCount + "秒再获取");
        InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次*/
        //向后台发送处理数据
        $.ajax({
            type: "POST", //用POST方式传输
            //dataType: "text", //数据格式:JSON
            url: '{:url("/code")}', //目标地址
            data: "dealType=" + dealType +"&username=" + username + "&code=" + code,
            error: function (XMLHttpRequest, textStatus, errorThrown) { },
            success: function (data){
                if (data.code)
                {
                    //设置button效果，开始计时
                    $("#btnSendCode").attr("disabled", "true");
                    $("#btnSendCode").val( + curCount + "秒再获取");
                    InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
                }
                layer.msg(data.msg);
            }
        });
    }
    //timer处理函数
    function SetRemainTime() {
        if (curCount == 0) {
            window.clearInterval(InterValObj);//停止计时器
            $("#btnSendCode").removeAttr("disabled");//启用按钮
            $("#btnSendCode").val("重新发送验证码");
            code = ""; //清除验证码。如果不清除，过时间后，输入收到的验证码依然有效
        }
        else {
            curCount--;
            $("#btnSendCode").val( + curCount + "秒再获取");
        }
    }

    function formSubmit() {
        var username = $("input[name='username']").val();
        var password = $("input[name='password']").val();
        var code = $("input[name='code']").val();
        if (username == '' || username == undefined) {
            layer.msg('请填写手机号码');
            return false;
        }
        if (password == '' || password == undefined) {
            layer.msg('请填写密码');
            return false;
        }
        if (code == '' || code == undefined) {
            layer.msg('请填写验证码');
            return false;
        }
        $.post("{:url('/register')}", {username:username,password:password,code:code}, function (data) {
            layer.msg(data.msg,{time: 3000});
            if (data.code) {
                setTimeout(function () {
                    location.href = '{:url("/user")}';
                },5000);
            }
        });
    }
</script>
{/block}